﻿using Models;

using SqlSugar;

namespace RESTfulDemo.Services {
    public class StudentInfoService : IService {
        private readonly ISqlSugarClient mSqlSugarClient;

        public StudentInfoService(ISqlSugarClient sqlSugarClient) {
            mSqlSugarClient = sqlSugarClient;
        }

        public async Task<IEnumerable<StudentInfo>> GetStudentInfosAsync() {
            return await mSqlSugarClient.Queryable<StudentInfo>().ToListAsync();
        }

        public async Task AddStudentInfoAsync(StudentInfo studentInfo) {
            await mSqlSugarClient.Insertable(studentInfo).ExecuteCommandAsync();
        }

        public async Task DeleteStudentInfoAsync(Guid id) {
            await mSqlSugarClient.Deleteable<StudentInfo>().In(id).ExecuteCommandAsync();
        }

        public async Task<StudentInfo> GetStudentInfoByIdAsync(Guid id) {
            return await mSqlSugarClient.Queryable<StudentInfo>().Where(it => it.Guid.Equals(id.ToString())).FirstAsync();
        }

        public async Task UpdateStudentInfoAsync(StudentInfo studentInfo) {
            await mSqlSugarClient.Updateable(studentInfo).ExecuteCommandAsync();
        }

        public async Task<bool> StudentExistAsync(Guid id) {
            return await mSqlSugarClient.Queryable<StudentInfo>().AnyAsync(it => it.Guid == id.ToString());
        }
    }
}